<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009 Christian Kartnig <office@hahnepeter.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');
require_once(PATH_formidableapi);

class topstip_pibase extends tslib_pibase{

	/**
	 * @var t3lib_DB $db
	 */
	var $db;

	/**
	 * @var tslib_fe $tsfe
	 */
	var $tsfe;

	/**
	 * The typoscript configuration for all plugins
	 * @var array
	 */
	protected $generalTS;

	private $tableTitles;
	private $tableNames;
	
	function __construct() {
		parent::__construct();
		$this->db = $GLOBALS['TYPO3_DB'];
		$this->tsfe = $GLOBALS['TSFE'];
		$this->generalTS = $this->tsfe->tmpl->setup['plugin.']['tx_topstip.'];
		$this->loadTables();
	}
	
	protected function getGeneralTS() {
		return $this->generalTS;
	}

	protected function loadTables(){
		$tableArray = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows ('*', 'tx_kbks_topstip_configuration', 'deleted=0');
		foreach($tableArray as $k => $v){
			$tableKey = $v['tx_kbks_tablekey'];
			$tableTitle = $v['tx_kbks_titel'];
			$tableName = $v['tx_kbks_tablename'];
			$this->tableNames[$tableKey] = $tableName;
			$this->tableTitles[$tableKey] = $tableTitle;
		}
	}

	protected function getQuery($selectFields,$where){
		$queryUnions = array();
		foreach($this->tableNames as $tableKey => $tableName){
			$tableTitle = $this->getTableTitle($tableKey);
			$queryUnions[] = "(SELECT	$selectFields,
										t.uid,
										s.tx_kbks_titel AS status_titel,
										t.tx_kbks_status,
										'$tableKey' AS tableKey,
										'$tableTitle' AS tableTitle,
										'$tableName' AS tableName
								FROM $tableName t
								INNER JOIN tx_kbks_status s ON t.tx_kbks_status = s.uid
								WHERE $where)";
		}
		$query = "SELECT * FROM (".implode('UNION', $queryUnions).") unionTable WHERE 1=1";
		return $query;
	}

	public function getFormUrl($dataRow){
		return $this->cObj->currentPageURL(
			 array(
				'tx_topstip_pi1[tableKey]' => $dataRow['tableKey'],
				'tx_topstip_pi1[uid]' => $dataRow['uid']
			 ),
			 $this->generalTS['pidForms']
		);
	}
	
	protected function getTableName($tableKey){
		return $this->tableNames[$tableKey];
	}
	protected function getTableTitle($tableKey){
		return $this->tableTitles[$tableKey];
	}

  protected function getPluginParam($fieldname){
		return $this->pi_getFFvalue($this->cObj->data['pi_flexform'], $fieldname);
	}	
	
	/**
	 * Returns true if fe_user has the rights to set this state
	 *
	 * @return	boolean
	 */
	protected function checkStatusUserRights($status) {
		$projektleiterStatesArray = explode(',', $this->conf['projektleiterStates']);
		return (in_array($status, $projektleiterStatesArray) && !$this->isProjektleiter()) ? false : true;
	}	

	/**
	 * Returns true if fe_user has admin rights
	 *
	 * @return	boolean
	 */
	function isAdmin() {
		$usergroupArray = explode(',', $GLOBALS['TSFE']->fe_user->user['usergroup']);
		$adminUsergroupsArray = explode(',', $this->conf['adminUsergroups']);
		$intersectArray = array_intersect($usergroupArray, $adminUsergroupsArray);
		return (sizeof($intersectArray) > 0);
	}

	/**
	 * Returns true if fe_user has special chief admin rights
	 *
	 * @return	boolean
	 */
	function isProjektleiter() {
		$usergroupArray = explode(',', $GLOBALS['TSFE']->fe_user->user['usergroup']);
		$adminUsergroupsArray = explode(',', $this->conf['projektleiterUsergroups']);
		$intersectArray = array_intersect($usergroupArray, $adminUsergroupsArray);	
		return (sizeof($intersectArray) > 0);
	}
	
	/**
	 * Returns an Array of Banks from the database
	 *
	 * @return	Array of Banks 
	 */
	function getBankArray() {
		return $GLOBALS['TYPO3_DB']->exec_SELECTgetRows ('*', 'tx_kbks_bank', 'deleted=0');
	}	
}
?>
